package com.zhiyou100.video.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhiyou100.video.model.Speaker;
import com.zhiyou100.video.service.SpeakerService;

@Controller
@RequestMapping("/admin")
public class SpeakerController {
	
	@Autowired
	private SpeakerService ss;
	
	@RequestMapping("/speaker")
	public String list(Model model) {
		List<Speaker> speaker = ss.selectAll();
		model.addAttribute("speakers", speaker);
		return "backstage/speaker/speaker";
	}
	
	@RequestMapping("/addspeaker")
	public String add() {
		return "backstage/speaker/edit_speaker";
	}
	
	@RequestMapping("/speaker/insert")
	public String insert(Speaker speaker,Model model) {
		Date date = new Date();
		speaker.setInsert_time(date);
		int i = ss.insertSpeaker(speaker);
		if(i>0) {
			return "redirect:../speaker";
		}else {
			return "backstage/speaker/edit_speaker";
		}
	}
	
	@RequestMapping("/select")
	public String selectByNameOrJob(Speaker speaker,Model model) {
		List<Speaker> speakers = ss.selectByNameOrJob(speaker);
		model.addAttribute("speakers", speakers);
		return "backstage/speaker/speaker";
	}
	
	@RequestMapping("/upspeaker")
	public String updatespeaker(int id,Model model) {
		Speaker speaker = ss.selectById(id);
		model.addAttribute("speaker",speaker);
		return "backstage/speaker/edit_speaker";
	}
	
	@RequestMapping("/speaker/update")
	public String update(Speaker speaker) {
		Date date = new Date();
		speaker.setUpdate_time(date);
		int i = ss.updateSpeaker(speaker);
		if(i>0) {
			return "redirect:../speaker";
		}
		return "/admin/speaker/update";
	}
	
	@RequestMapping("/speaker/delete")
	public String deleteById(int id) {
		int i = ss.deleteById(id);
		if(i>0) {
			return "redirect:../speaker";
		}
		return null;
	}
	
}
